const app = getApp()
const url = app.globalData.baseUrl
import Toast from '@vant/weapp/toast/toast'

//get请求
export function getRequest(suffixUrl, params, callbackOk, callbackFail) {
	const token = wx.getStorageSync('token')
	if (token == null || token == '') {
		wx.reLaunch({
			url: '/loginpackage/pages/login/login',
		})
	} else {
		wx.request({
			url: url + suffixUrl,
			method: 'GET',
			header: {
				'token': token
			},
			data: params,
			success: function (res) {
				if (res.data.code == 401) {
					wx.reLaunch({
						url: '/loginpackage/pages/login/login',
					})
				} else {
					callbackOk(res.data)
				}
			},
			fail: function (e) {
				callbackFail(e);
				wx.showToast({
					title: '加载失败...',
					icon: 'none'
				})
			},
			complete: function () {
				Toast.clear()
			}
		})
	}
}

//get请求类型参数设置
export function getTypeRequest(suffixUrl, contentType, params, callbackOk, callbackFail) {
	const token = wx.getStorageSync('token')
	if (token == null || token == '') {
		wx.reLaunch({
			url: '/loginpackage/pages/login/login',
		})
	} else {
		wx.request({
			url: url + suffixUrl,
			method: 'GET',
			header: {
				'token': token,
				'content-type': contentType
			},
			data: params,
			success: function (res) {
				if (res.data.code == 401) {
					wx.reLaunch({
						url: '/loginpackage/pages/login/login',
					})
				} else {
					callbackOk(res.data)
				}
			},
			fail: function (e) {
				callbackFail(e)
				wx.showToast({
					title: '加载失败...',
					icon: 'none'
				})
			},
			complete: function () {
				Toast.clear()
			}
		})
	}
}

//post请求
export function postRequest(suffixUrl, data, callbackOk, callbackFail) {
	const token = wx.getStorageSync('token')
	if (token == null || token == '') {
		wx.reLaunch({
			url: '/loginpackage/pages/login/login',
		})
	} else {
		wx.request({
			url: url + suffixUrl,
			method: 'POST',
			header: {
				'token': token
			},
			data: data,
			success: function (res) {
				if (res.data.code == 401) {
					wx.reLaunch({
						url: '/loginpackage/pages/login/login',
					})
				} else {
					callbackOk(res.data)
				}
			},
			fail: function (e) {
				callbackFail(e)
				wx.showToast({
					title: '加载失败...',
					icon: 'none'
				})
			},
			complete: function () {
				Toast.clear()
			}
		})
	}
}

//post请求参数类型设置
export function postTypeRequest(suffixUrl, contentType, data, callbackOk, callbackFail) {
	const token = wx.getStorageSync('token')
	if (token == null || token == '') {
		wx.reLaunch({
			url: '/loginpackage/pages/login/login',
		})
	} else {
		wx.request({
			url: url + suffixUrl,
			method: 'POST',
			header: {
				'token': token,
				'content-type': contentType
			},
			data: data,
			success: function (res) {
				if (res.data.code == 401) {
					wx.reLaunch({
						url: '/loginpackage/pages/login/login',
					})
				} else {
					callbackOk(res.data)
				}
			},
			fail: function (e) {
				callbackFail(e)
				wx.showToast({
					title: '加载失败...',
					icon: 'none'
				})
			},
			complete: function () {
				Toast.clear()
			}
		})
	}

}